from django.shortcuts import render
from django.http import HttpResponse


# Create your views here.

def set_cookie(request):
    resp = HttpResponse("设置cookie成功!!")
    # resp.set_cookie("name", "python", max_age=10)
    resp.set_cookie("name", "python", expires=7 * 24 * 60 * 60)  # 信息保存一周
    return resp


def get_cookie(request):
    name = request.COOKIES.get("name")
    return HttpResponse(f"获取到的cookie信息为name{name}")


def del_cookie(request):
    resp = HttpResponse("删除COOKIE成功!!")
    # resp.delete_cookie("name")  # 删除name的cookie
    # 删除所有的cookie值
    request.COOKIES.clear()

    return resp


def set_session(request):
    request.session["name1"] = "zyd"
    request.session["name2"] = "cmy"
    return HttpResponse("设置session成功!!")


def get_session(request):
    name1 = request.session.get("name1")
    name2 = request.session.get("name2")
    return HttpResponse(f"获取到的session值为name1{name1},name2{name2}")


def del_session(request):
    del request.session["name1"]  # 删除某一个key
    del request.session["name2"]
    request.session.clear()  # 删除数据,保留session key
    request.session.flush()  # 删除数据,不保留key,删除整个会话
    resp = HttpResponse("删除session成功!!")
    return resp
